package com.ct.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.StaticLog;
import com.ct.event.AsyncSendEmailEvent;
import com.ct.service.OrderAsyncService;
import com.google.common.util.concurrent.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

@RestController
@RequestMapping("/async")
public class ThreadController {

    @Autowired
    public OrderAsyncService orderAsyncService;

    @Autowired
    private ApplicationContext applicationContext;


    private ExecutorService executorService = Executors.newCachedThreadPool();


    @SneakyThrows
    @GetMapping("/test1")
    public String test() {
        StaticLog.info("1-satrt");

        Future<String> submite2 = executorService.submit(() -> {
            StaticLog.info("e-2");
            return "e2success";
        });

        Future<String> submite3 = executorService.submit(() -> {
            ThreadUtil.sleep(5000);
            StaticLog.info("e-3");
            return "e3success";
        });


        ThreadUtil.sleep(2000);
        StaticLog.info(submite2.get());
//        StaticLog.info(submite3.get());
        StaticLog.info("1-end");

        return "success";
    }


    @SneakyThrows
    @GetMapping("/test2")
    public String test2() {
        StaticLog.info("1-satrt");
        orderAsyncService.sendMessage(1L);
        StaticLog.info("2-end");
        orderAsyncService.socreGet(2L);
        return "success";
    }


    @SneakyThrows
    @GetMapping("/test3")
    public String test3() {
        applicationContext.publishEvent(new AsyncSendEmailEvent(1L));
        return "success";
    }




    @SneakyThrows
    @GetMapping("/test4")
    public String test4() {
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {
                                                                                      @Override
                                                                                      public Integer call() throws Exception {
                                                                                          StaticLog.info("callable execute...");
                                                                                          TimeUnit.SECONDS.sleep(1);
                                                                                          return 1;
                                                                                      }
                                                                                  }
        );


        Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
            @Override
            public void onSuccess(Integer result) {
                //成功执行...
                System.out.println("Get listenable future's result with callback " + result);
            }

            @Override
            public void onFailure(Throwable t) {
                //异常情况处理...
                t.printStackTrace();
            }
        });
        return "success";
    }
}
